//
//  MainViewController.swift
//  新浪微博
//
//  Created by 焦林生 on 15/11/3.
//  Copyright © 2015年 焦林生. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    //MARK: 监听方法
    //如果使用@objc修饰符 可以保证运行循环能够发送此消息,即使是private
    @objc private func clickComposeButton(){
    
        //判断用户是否登录
        var vc: UIViewController
        if UserAccountViewModel.sharedUserAccount.userLogin {
        
            vc = ComposeViewController()
        }else {
        
            vc = OAuthViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        /**
        添加控制器 不会创建tababr中的按钮,根据图片的大小自动布局
        懒加载是无处不在 所有控件都是延迟创建
        */
        addChildViewController()
        
        setupComposeButton()
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        //会创建tabbar中所有控制器对应的按钮
        super.viewWillAppear(animated)
        
        //将撰写按钮弄到最前面
        tabBar.bringSubviewToFront(composeButton)
    }
    
    //MARK: 懒加载控件
    private lazy var composeButton:UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    
    
}

//MARK: - 设置界面 
//把相关联的代码放在一起 使用方便
extension MainViewController{
    
    //设置撰写按钮
    private func setupComposeButton(){
    
        //1添加按钮
        tabBar.addSubview(composeButton)
        //2 调整按钮
        let count = childViewControllers.count
        //
        let w = tabBar.bounds.width / CGFloat(count)-1
        
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        //3添加监听方法
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
        
    }

    //添加所有的控制器
    private func addChildViewController() {
        
        //设置tintcolor  图片的渲染
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    //添加控制器
    private func addChildViewController(vc: UIViewController,title: String, imageName: String ) {
        
        //设置标题  由内至外设置的
        vc.title = title
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //导航控制器(vc是导航控制器的跟控制器)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }

}
